//
//  DockManager.m
//  McRemoteServer
//
//  Created by Magic on 2/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DockManager.h"

@implementation DockManager {
    NSMutableDictionary *_dockAppNameAndPath;
    NSMutableDictionary *_launchedAppNameAndInstance;
}

+ (DockManager *)sharedManager
{
    static DockManager *sharedManager;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedManager = [[DockManager alloc] init];
    });
    
    return sharedManager;
}

- (id)init {
    self = [super init];
    if (self) {
        _dockAppNameAndPath = [[NSMutableDictionary alloc] initWithCapacity:10];
        _launchedAppNameAndInstance = [[NSMutableDictionary alloc] initWithCapacity:10];
        
    }
    return self;
}

- (void)setDockAppName:(NSString *)name path:(NSString *)path
{
    [_dockAppNameAndPath setObject:path forKey:name];
}

- (void)setDockAppName:(NSString *)name instance:(NSRunningApplication *)app
{
    [_launchedAppNameAndInstance setObject:app forKey:name];
}

- (void)terminateAppNamed:(NSString *)name
{
    [_launchedAppNameAndInstance removeObjectForKey:name];
}

- (void)launchAppNamed:(NSString *)name
{
    NSString *path = [_dockAppNameAndPath objectForKey:name];
    if (path) {
        [[NSWorkspace sharedWorkspace] launchApplication:path];
    }
    else {
        NSRunningApplication *app = [_launchedAppNameAndInstance objectForKey:name];
        if (app) {
            [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
        }
        
    }
    
}

- (NSData *)iconDataWithAppName:(NSString *)name retina:(BOOL)retina
{
    NSString *path = [_dockAppNameAndPath objectForKey:name];
    //[NSString stringWithFormat:@"/Applications/%@.app",appName];
    //[[NSWorkspace sharedWorkspace] fullPathForApplication:appName];
    NSImage *icon;
    if (path) {
        icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
        NSLog(@"dock app path:%@",path);
    }
    else { 
        NSLog(@"no app path for:%@",name);
        NSRunningApplication *runningApp = [_launchedAppNameAndInstance objectForKey:name];
        icon = [runningApp icon];
    }
   
    
    if (retina) {
        [icon setSize:NSMakeSize(128, 128)];
    }
    else {
        [icon setSize:NSMakeSize(64, 64)];
    }
    
    
    [icon lockFocus];
    
    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
                             
                             NSMakeRect(0,0,icon.size.width,icon.size.height)];
    
    [icon unlockFocus]; 
    
    NSData *iconData = [rep representationUsingType:NSPNGFileType properties:nil];
    [rep release];
    return iconData;
}

- (void)getIconDataWithAppName:(NSString *)name retina:(BOOL)retina finishBlock:(DockManagerGetIconBlock)finishBlock
{
    
    NSString *path = [_dockAppNameAndPath objectForKey:name];
    
    dispatch_queue_t queue = dispatch_queue_create("GetIconData", NULL);
    dispatch_async(queue, ^{        
        
        NSImage *icon;
        if (path) {
            icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
            NSLog(@"dock app path:%@",path);
        }
        else {
            NSRunningApplication *runningApp = [_launchedAppNameAndInstance objectForKey:name];
            icon = [runningApp icon];
        }
        
        if (retina) {
            [icon setSize:NSMakeSize(128, 128)];
        }
        else {
            [icon setSize:NSMakeSize(64, 64)];
        }
        
        [icon lockFocus];
        
        NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
                                 
                                 NSMakeRect(0,0,icon.size.width,icon.size.height)];
        
        [icon unlockFocus]; 
        
        NSData *iconData = [rep representationUsingType:NSPNGFileType properties:nil];
        [rep release];
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            finishBlock(iconData);
        });
        
        
        
    });
    
    
    dispatch_release(queue);
    
}

@end
